//

import UIKit


class CZWelcomeController: UIViewController {

    //懒加载背景
    private lazy var bgImageView:UIImageView =  {
        let bgImageView = UIImageView(image: (UIImage(named: "ad_background")))
        return bgImageView
    }()
    
    //懒加载头像
    private lazy var iconImgView: UIImageView = {
        let iconImgView = UIImageView(image: (UIImage(named: "avatar_default_big")))
        iconImgView.layer.masksToBounds = true
        iconImgView.layer.cornerRadius = 42.5
        //判断头像是否为nil
        if let imageURL = CZUserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
            //图像有地址
           
        }
        
        
        return iconImgView
    }()

    //懒加载文字
    private lazy var messageLabel: UILabel = {
       let messageLabel = UILabel()
    
        messageLabel.font = UIFont.systemFontOfSize(17)
        messageLabel.textColor = UIColor.grayColor()
        if let name = CZUserAccountViewModel.shareUserAccount.userAccount?.name {
                messageLabel.text = "欢迎回来\(name)"
        } else {
            messageLabel.text = "欢迎回来"
        }
        
        //设置透明度
        messageLabel.alpha = 0
        return messageLabel
    }()
    
    //loadView 自定义视图
    override func loadView() {
        view = bgImageView
    }
    
    //设置约束
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpView()
    }
    //执行动画应该在即将展示
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        //动画
        startAnimation()
    }
    
    private func setUpView() {
        //先添加子控件
        view.addSubview(iconImgView)
        view.addSubview(messageLabel)
        
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImgView.snp_bottom).offset(20)
            
        }
    }
    
    //动画效果
    private func startAnimation() {
      //设置头像的约束
        iconImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        //执行动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //执行约束动画
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    //设置透明度
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进去首页
                        NSNotificationCenter.defaultCenter().postNotificationName(switfRootViewController, object: nil)
                })
        }
        
        
    }
    
}














